package com.yj.springbootstudy;

import com.yj.springbootstudy.config.User;
import com.yj.springbootstudy.config.WebConfig;
import com.yj.springbootstudy.config.profile.CalculateService;
import com.yj.springbootstudy.config.profile.Hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Arrays;

@SpringBootApplication
public class SpringBootStudyApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringBootStudyApplication.class, args);
//
//
//        //使用注解配置，传入注解类
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(WebConfig.class);
        System.out.println("容器创建完成！");
//
//
//        User user1 = (User) applicationContext.getBean("user");
//        User user2 = (User) applicationContext.getBean("user");
//        System.out.println(user2 == user1);
//
//        //查看User 这个类在Spring 容器中叫啥
//        String[] beanNamesForType = applicationContext.getBeanNamesForType(User.class);
//        System.out.println(Arrays.toString(beanNamesForType));
//
//        //查看基于注解IOC容器中所有组件名称
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        System.out.println(Arrays.toString(beanDefinitionNames));

        Hello hello = applicationContext.getBean(Hello.class);
        hello.hello();

        //如果不加& 获取的FactoryBean getObject 对象
        //加了& 获取的则是 FactoryBean 对象
        Object cherryFactory = applicationContext.getBean("&cherryFactory");
        System.out.println(cherryFactory);

        //todo
//        ConfigurableApplicationContext context1 = new SpringApplicationBuilder(SpringBootStudyApplication.class)
//                .web(WebApplicationType.NONE)
//                .profiles("java8")
//                .run(args);
//
//        CalculateService calculateService = context1.getBean(CalculateService.class);
//        Integer sum = calculateService.sum(1, 2, 3, 4, 5, 6, 7, 8);
//        System.out.println(sum);


    }

}
